<?php

/**
 * @copyright   FOSP
 * @version     20120327
 * @todo        Access table statictis by MySQL
 * @name        Fpt_Business_Statictis_Adapter_Mysql
 * @author      QuangTM 
 */
class Fpt_Business_Statictis_Adapter_Mysql extends Fpt_Business_Statictis_Adapter_Abstract
{

    /**
     * Get Fpt_Business_Statictis_Adapter_Mysql's instance
     * @return Fpt_Business_Statictis_Adapter_Mysql
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get list article like by site thethao
     * @param int $limit
     * @return array|boolean
     * @author QuangTM 
     */
    public function getListArticleLikeBySiteID($limit = 10)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_getArticleLikeBySiteID(:p_limit);');

            // Bind param
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $row['article_id']);

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get list article by comment
     * @param int $limit
     * @return array|boolean
     * @author QuangTM 
     */
    public function getListArticleByComment($limit = 10)
    {
        try
        {
            $result = array();
            // Get DB Obj
            $dbObj = Fpt_Global::getSportDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_geListtarticleByComment(:p_limit);');

            // Bind param
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $row['article_id']);

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * get list article by view
     * @param int $limit
     * @return array|boolean
     * @author QuangTM 
     */
    public function getListArticleByView($articleType = NULL, $cateIDs = NULL, $limit = 10 )
    {
        try
        {
            $result = array();
            // Get DB Obj
            $dbObj = Fpt_Global::getSportDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_getListtarticleByView(:p_articletype, :p_cateid, :p_limit);');

            // Bind param
            $stmt->bindParam('p_articletype', $articleType);
            $stmt->bindParam('p_cateid', $cateIDs);
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);
            // Execute
            $stmt->execute();
           
            // Get result
            $result = array();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $row['article_id']);

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}